package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strings"
)
// 本例简单演示路由基本功能
// 路由模块化 见day02包
func main (){
	router  := gin.Default()
	router.GET("/ping",func(context *gin.Context){
		context.JSON(200,gin.H{"msg":"pong"})
	})

	router.GET("/someJSON",func(context *gin.Context){
		data :=map[string]interface{}{ "lang":"GO语言","tag":"<br>", }
		context.AsciiJSON(http.StatusOK,data)
	})

	router.LoadHTMLGlob("templates/**/*")
	router.GET("/index", func(context *gin.Context) {
		context.HTML(http.StatusOK,"index.tmpl",gin.H{ "title": "Main website",})
	})
	router.GET("posts/index",func(context *gin.Context){
		context.HTML(http.StatusOK,"/posts/index.tmpl",gin.H{ "title": "posts",})
	})
	router.GET("users/index",func(context *gin.Context){
		context.HTML(http.StatusOK,"/users/index.tmpl",gin.H{ "title": "users",})
	})
	// rustFull 路由
	router.GET("/user/:name/*action", func(context *gin.Context) {
		name := context.Param("name")
		action := context.Param("action")
		//截取/
		action = strings.Trim(action, "/")
		context.String(http.StatusOK, name+" is "+action)
	})
	// 获取url 参数
	// DefaultQuery()若参数不存在，返回默认值，Query()若不存在，返回空串
	router.GET("/user", func(context *gin.Context) {
		//指定默认值
		//http://localhost:8080/user 才会打印出来默认的值
		name := context.DefaultQuery("name", "枯藤")
		context.String(http.StatusOK, fmt.Sprintf("hello %s", name))
	})
	// 表单参数解析
	// 可以通过PostForm()方法获取
	router.POST("/form", func(context *gin.Context) {
		types := context.DefaultPostForm("type", "post")
		username := context.PostForm("username")
		password := context.PostForm("userpassword")
		// c.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s,type:%s", username, password, types))
		context.String(http.StatusOK, fmt.Sprintf("username:%s,password:%s,type:%s", username, password, types))
	})

	//限制上传最大尺寸
	router.MaxMultipartMemory = 8 << 20

	// 文件上传之单个文件
	router.POST("/upload", func(context *gin.Context) {
		file, err := context.FormFile("file")
		if err != nil {
			context.String(500, "上传图片出错")
		}
		// c.JSON(200, gin.H{"message": file.Header.Context})
		context.SaveUploadedFile(file, file.Filename)
		context.String(http.StatusOK, file.Filename)
	})

	// 文件上传之多个文件
	router.POST("/uploads", func(context *gin.Context) {
		form, err := context.MultipartForm()
		if err != nil {
			context.String(http.StatusBadRequest, fmt.Sprintf("get err %s", err.Error()))
		}
		// 获取所有图片
		files := form.File["files"]
		// 遍历所有图片
		for _, file := range files {
			// 逐个存
			if err := context.SaveUploadedFile(file, file.Filename); err != nil {
				context.String(http.StatusBadRequest, fmt.Sprintf("upload err %s", err.Error()))
				return
			}
		}
		context.String(200, fmt.Sprintf("upload ok %d files", len(files)))
	})

	//路由分组
	v1 := router.Group("/v1")
	{
		v1.GET("/login", login)
		v1.POST("submit", submit)
	}
	v2 := router.Group("/v2")
	{
		v2.GET("/login", login)
		v2.POST("/submit", submit)
	}
	router.Run(":8080")
}
func login(c *gin.Context) {
	name := c.DefaultQuery("name", "jack")
	c.String(200, fmt.Sprintf("hello %s\n", name))
}

func submit(c *gin.Context) {
	name := c.DefaultQuery("name", "lily")
	c.String(200, fmt.Sprintf("hello %s\n", name))
}